package dmt16.TelemetrySuit.UI
{
	import dmt16.TelemetrySuit.UI.Tiles.TileBase;
	import dmt16.TelemetrySuit.UI.Tiles.TileEvent;
	
	import flash.display.DisplayObject;
	import flash.display.NativeWindow;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.core.ScrollControlBase;
	
	public class MainWindow extends Sprite
	{		
		public function MainWindow()
		{
			if(!_instance){
				_instance=this;
				
				addEventListener(Event.ADDED_TO_STAGE,_onAddedToStage);
				
				addEventListener(Event.ENTER_FRAME,_onEnterFrame);
				
				_mapView=new MapView();
				_tileView=new TileContainer();
				_detailView=new DetailView();
				
				_tileView.addEventListener(TileEvent.TILE_SELECT,_onTileSelect);
				
				_connectBtn=new ConnectBtn();
				_connectBtn.width=160;
				_connectBtn.height=40;
				
				_stopWatch=new StopWatch();
				
				addChild(_mapView);
				addChild(_tileView);
				addChild(_detailView);
				addChild(_stopWatch);
				
				addChild(_connectBtn);
			}
			else{
				throw new Error("Only one instance of MainWindow can be run at a time");
			}
		}
		
		private static var _instance:MainWindow;
		public static function get instance():MainWindow{
			return _instance;
		}
		
		private function _onTileSelect(event:TileEvent):void{
			if(mainView){
				mainView=false;
				_detailView.giveControl(event.tile);
			}
		}
		
		private function _onAddedToStage(event:Event):void{
			stage.addEventListener(Event.RESIZE,_onResize);
			
		}
		
		private function _toggleView(event:MouseEvent):void{
			mainView=!mainView;
		}
		
		private function _onEnterFrame(event:Event):void{
			var tweenPos:Number=stage.stageWidth*_tweenPos;
			_mapView.x=(stage.stageWidth-_mapView.width)-tweenPos-1;
			_mapView.y=0;
			
			_tileView.x=0-tweenPos;
			_tileView.y=60;
			
			_detailView.x=stage.stageWidth-tweenPos;
			_detailView.y=0;
			
			_stopWatch.x=(stage.stageWidth-_stopWatch.width)-tweenPos-1;
			_stopWatch.y=stage.stageHeight-_stopWatch.height;		
			
			_connectBtn.x=20-tweenPos;
			_connectBtn.y=20;
			
			_tweenPos+=((mainView?0:1)-_tweenPos)*0.2;
			if(Math.abs((mainView?0:1)-_tweenPos)<0.0001)
				_tweenPos=(mainView?0:1);
		}
		
		private var _tweenPos:Number=0;
		public var mainView:Boolean=true;
		
		private function _onResize(event:Event):void{
			graphics.beginFill(TileBase.BG_COLOUR);
			graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
			
			_mapView.height=stage.stageHeight;
			_mapView.width=stage.stageWidth/3;
			
			_stopWatch.width=stage.stageWidth/3;
			_stopWatch.height=115;
			
			_tileView.height=stage.stageHeight-60;
			_tileView.width=2*stage.stageWidth/3;
			
			_detailView.width=stage.stageWidth;
			_detailView.height=stage.stageHeight;
			
			_mapView.dispatchEvent(new Event(Event.RESIZE));
			_tileView.dispatchEvent(new Event(Event.RESIZE));
			_detailView.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private var numTiles:Number=0;
		
		public override function addChild(child:DisplayObject):DisplayObject{
			child.x=120*(numTiles%5)+40;
			child.y=120*Math.floor(numTiles/5)+40;
			numTiles++;
			return super.addChild(child);
		}
		
		private var _mapView:MapView;
		public function get mapView():MapView{
			return _mapView;
		}
		
		private var _stopWatch:StopWatch;
		public function get stopWatch():StopWatch{
			return _stopWatch;
		}
		
		private var _tileView:TileContainer;
		public function get tileView():TileContainer
		{
			return _tileView;
		}
		
		private var _detailView:DetailView;
		public function get detailView():DetailView
		{
			return _detailView;
		}
		
		private var _connectBtn:ConnectBtn;
		public function get connectBtn():ConnectBtn
		{
			return _connectBtn;	
		}
	}
}